home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 551-575 / disk_570 / gadtoolsbox / source / source.lha / func.c < prev    next >
C/C++ Source or Header  |  1991-11-04  |  24KB  |  891 lines

  1. /*-- AutoRev header do NOT edit!
  2. *
  3. *   Program         :   func.c
  4. *   Copyright       :   © Copyright 1991 Jaba Development
  5. *   Author          :   Jan van den Baard
  6. *   Creation Date   :   21-Sep-91
  7. *   Current version :   1.00
  8. *   Translator      :   DICE v2.6
  9. *
  10. *   REVISION HISTORY
  11. *
  12. *   Date          Version         Comment
  13. *   ---------     -------         ------------------------------------------
  14. *   21-Sep-91     1.00            Several functions.
  15. *
  16. *-- REV_END --*/
  17.  
  18. #include "GTEd.h"
  19. #include "Protos.h"
  20.  
  21. /*
  22.  * Refrenced external global data.
  23.  */
  24. extern ULONG                 Class;
  25. extern UWORD                 Qualifier, Code;
  26. extern APTR                  Object, MainVisualInfo;
  27. extern struct TextAttr       MainFont;
  28. extern struct MemoryChain   *Misc, *Chain;
  29. extern struct Screen        *MainScreen;
  30. extern struct Window        *MainWindow;
  31. extern struct RastPort      *MainRP;
  32. extern struct ExtGadgetList  Gadgets;
  33. extern struct Gadget        *MainGList;
  34. extern struct TagItem        MainSTags[];
  35. extern struct Screen        *MainScreen;
  36. extern BOOL                  WBenchClose;
  37. extern UBYTE                 MainWBStatus[20], MainFileName[512];
  38. extern struct ExtGadgetList  Gadgets;
  39. extern UWORD                 CountFrom;
  40. extern UBYTE                *MainExtension;
  41. extern struct StringExtend   Sextend;
  42. extern BOOL                  GadgetsOn;
  43. extern struct Gadget        *MainGList;
  44. extern struct TagItem        nwTags[];
  45. extern struct NewMenu        Menus[];
  46. extern struct Menu          *MainMenus;
  47. extern struct TextAttr       Topaz80;
  48. extern BOOL                  BreakDRAG;
  49. extern struct Prefs          MainPrefs;
  50. extern struct IntuiText     *WindowTxt;
  51. extern BOOL                  Saved;
  52. extern UWORD                 AlertCol;
  53. extern UBYTE                 bi_SFile[], bi_LFile[];
  54.  
  55. /*
  56.  * --- Reads a message from the window message port.
  57.  * --- Returns TRUE if a message was read and puts the
  58.  * --- message data in the globals. Return FALSE if there
  59.  * --- was no message at the port.
  60.  */
  61. long ReadIMsg( struct Window *iwnd )
  62. {
  63.     struct IntuiMessage *imsg;
  64.  
  65.     if ( imsg = GT_GetIMsg( iwnd->UserPort )) {
  66.  
  67.         Class       =   imsg->Class;
  68.         Qualifier   =   imsg->Qualifier;
  69.         Code        =   imsg->Code;
  70.         Object      =   imsg->IAddress;
  71.  
  72.         if ( Class == IDCMP_MENUVERIFY )
  73.             MouseMove( MainWindow, FALSE );
  74.  
  75.         GT_ReplyIMsg( imsg );
  76.  
  77.         return TRUE;
  78.     }
  79.     return FALSE;
  80. }
  81.  
  82. /*
  83.  * --- Clears all message from a message port.
  84.  */
  85. void ClearMsgPort( struct MsgPort *mport )
  86. {
  87.     struct IntuiMessage  *msg;
  88.  
  89.     while ( msg = GT_GetIMsg( mport )) GT_ReplyIMsg( msg );
  90. }
  91.  
  92. /*
  93.  * --- Create a node with a special userdata field used
  94.  * --- for the listview gadgets of the program. The allocated
  95.  * --- node also makes 100 bytes room for the node name.
  96.  */
  97. struct ListViewNode *MakeNode( UBYTE *name )
  98. {
  99.     struct ListViewNode     *node;
  100.  
  101.     if ( node = ( struct ListViewNode * )AllocItem( Misc, (long)sizeof( struct ListViewNode ), MEMF_PUBLIC )) {
  102.         node->ln_Name = &node->ln_NameBytes[0];
  103.         strcpy( &node->ln_NameBytes[0], name );
  104.         return( node );
  105.     }
  106.     return( NULL );
  107. }
  108.  
  109. /*
  110.  * --- Find node 'entry' in list 'list' and
  111.  * --- return a pointer to it.
  112.  */
  113. struct ListViewNode *FindNode( struct List *list , long entry )
  114. {
  115.     struct ListViewNode     *node;
  116.     long                     num;
  117.  
  118.     for ( node = ( struct ListViewNode * )list->lh_Head, num = 0; num != entry; num++, node = node->ln_Succ );
  119.  
  120.     return( node );
  121. }
  122.  
  123. /*
  124.  * --- Free a complete list of nodes allocated with MakeNode();
  125.  */
  126. void FreeList( struct List *list )
  127. {
  128.     struct ListViewNode     *node;
  129.  
  130.     while ( node = ( struct ListViewNode * )RemHead( list ))
  131.         FreeItem( Misc, node, (long)sizeof( struct ListViewNode ));
  132. }
  133.  
  134. /*
  135.  * --- Read the current mouse coordinates. This
  136.  * --- routines also gives correct coordinates
  137.  * --- on GIMMEZEROZERO windows.
  138.  */
  139. void GetMouseXY( UWORD *x, UWORD *y )
  140. {
  141.     ULONG   wflg;
  142.  
  143.     Forbid();
  144.     wflg = MainWindow->Flags;
  145.     Permit();
  146.  
  147.     if (( wflg & WFLG_GIMMEZEROZERO ) == WFLG_GIMMEZEROZERO ) {
  148.         *x = MainWindow->GZZMouseX;
  149.         *y = MainWindow->GZZMouseY;
  150.     } else {
  151.         *x = MainWindow->MouseX;
  152.         *y = MainWindow->MouseY;
  153.     }
  154. }
  155.  
  156. /*
  157.  * --- Draw a complemented box in the window.
  158.  */
  159. void Box( UWORD x, UWORD y, UWORD x1, UWORD y1 )
  160. {
  161.     UWORD   tmp;
  162.  
  163.     if ( x > x1 )   { tmp = x; x = x1; x1 = tmp; }
  164.     if ( y > y1 )   { tmp = y; y = y1; y1 = tmp; }
  165.  
  166.     SetDrMd( MainRP, JAM1 + COMPLEMENT );
  167.  
  168.     Move( MainRP, x + 1, y );
  169.     Draw( MainRP, x1, y );
  170.     Draw( MainRP, x1, y1 );
  171.     Draw( MainRP, x, y1 );
  172.     Draw( MainRP, x, y );
  173. }
  174.  
  175. /*
  176.  * --- Allocate a TagItem array.
  177.  */
  178. struct TagItem  *MakeTagList( long numtags )
  179. {
  180.     struct TagItem  *tags;
  181.  
  182.     if ( tags = ( struct TagItem * )AllocItem( Chain, (long)( sizeof( struct TagItem ) * ( numtags + 1 )), MEMF_PUBLIC )) {
  183.         tags[ numtags ].ti_Tag = TAG_DONE;
  184.         return( tags );
  185.     }
  186.     return NULL;
  187. }
  188.  
  189. /*
  190.  * --- Set the Tag data of a Tag in a TagItem array.
  191.  */
  192. long SetTagData( struct TagItem *tags, Tag tag, Tag data )
  193. {
  194.     while ( tags->ti_Tag != tag ) {
  195.         if ( tags->ti_Tag == TAG_DONE )
  196.             return FALSE;
  197.         tags++;
  198.     }
  199.     tags->ti_Data = data;
  200.     return TRUE;
  201. }
  202.  
  203. /*
  204.  * --- Find the Tag in a TagItem array.
  205.  * --- The original TagInArray scans the data too which
  206.  * --- results in a TAG_DONE when the data is 0. I don't want this.
  207.  */
  208. long MyTagInArray( Tag tag, struct TagItem *tags )
  209. {
  210.     while ( tags->ti_Tag != tag ) {
  211.         if ( tags->ti_Tag == TAG_DONE )
  212.             return FALSE;
  213.         tags++;
  214.     }
  215.     return TRUE;
  216. }
  217.  
  218. /*
  219.  * --- Deallocate a TagItem array.
  220.  */
  221. void FreeTagList( struct TagItem *tags, long numtags )
  222. {
  223.     FreeItem( Chain, tags, (long)( sizeof( struct TagItem ) * ( numtags + 1 )));
  224. }
  225.  
  226. /*
  227.  * --- Wait for the user to either click on a gadget or press ESC.
  228.  */
  229. struct Gadget *WaitForGadget( struct Window *wnd )
  230. {
  231.     struct Gadget   *g;
  232.     BOOL             mm = FALSE;
  233.  
  234.     mm = MouseMove( wnd, FALSE );
  235.  
  236.     while ( 1 ) {
  237.         WaitPort( wnd->UserPort );
  238.         while( ReadIMsg( wnd )) {
  239.             if ( Class == IDCMP_RAWKEY && Code == 0x45 ) {
  240.                 ClearMsgPort( wnd->UserPort );
  241.                 return NULL;
  242.             } else if ( Class == IDCMP_GADGETUP || Class == IDCMP_GADGETDOWN ) {
  243.                 g = ( struct Gadget * )Object;
  244.                 goto check;
  245.             }
  246.         }
  247.     }
  248.  
  249.     check:
  250.  
  251.     if ( Class == IDCMP_GADGETDOWN ) {
  252.         while( Code != SELECTUP ) {
  253.             while ( ReadIMsg( wnd ));
  254.         }
  255.     }
  256.  
  257.     ClearMsgPort( wnd->UserPort );
  258.  
  259.     if ( mm ) MouseMove( wnd, TRUE );
  260.  
  261.     return( g );
  262. }
  263.  
  264. /*
  265.  * --- (Re)Open the screen. This routine opens the
  266.  * --- used font found in the TextAttr structure MainFont
  267.  * --- because GadTools seems to need to be able to open the font
  268.  * --- using OpenFont() to make some gadgets (Integer & String).
  269.  * --- To let gadtools be able to OpenFont() the font it has
  270.  * --- to be in memory first. So by opening the font before the
  271.  * --- screen is opened the font is in memory and openable with
  272.  * --- OpenFont(). The routine closes the font as soon as all
  273.  * --- gadgets are created. It took me quite a while to figure this
  274.  * --- out because I could write and read a binary OK. But when I
  275.  * --- try'ed to load the same binary the next day GadTools would refuse
  276.  * --- to create the gadgets in the file! And then it struck me......
  277.  * --- The screen did not use the font specified in the file!!!!!
  278.  */
  279. long ReOpenScreen( long wnd )
  280. {
  281.     struct TextFont     *tf = 0l;
  282.     long                 mm = FALSE;
  283.  
  284.     if ( MainWindow ) {
  285.         mm = MouseMove( MainWindow, FALSE );
  286.         if ( MainMenus ) {
  287.             ClearMenuStrip( MainWindow );
  288.             FreeMenus( MainMenus );
  289.             BreakDRAG = TRUE;
  290.             MainMenus = 0l;
  291.         }
  292.         RemoveAllGadgets();
  293.  
  294.         if ( NOT wnd ) {
  295.             nwTags[0].ti_Data   =   (Tag)MainWindow->LeftEdge;
  296.             nwTags[1].ti_Data   =   (Tag)MainWindow->TopEdge;
  297.             nwTags[2].ti_Data   =   (Tag)MainWindow->Width;
  298.             nwTags[3].ti_Data   =   (Tag)MainWindow->Height;
  299.         } else if ( wnd == TRUE ) {
  300.             nwTags[0].ti_Data   =   10l;
  301.             nwTags[1].ti_Data   =   15l;
  302.             nwTags[2].ti_Data   =   200l;
  303.             nwTags[3].ti_Data   =   50l;
  304.         }
  305.  
  306.         CloseWindow( MainWindow );
  307.         MainWindow = 0l;
  308.     }
  309.  
  310.     if ( ModeNotAvailable( MainSTags[5].ti_Data )) {
  311.         MyRequest( "Hello, hello..", "OK", "File uses monitor that isn't available.\n    I will use the default monitor!" );
  312.         MainSTags[5].ti_Data = DEFAULT_MONITOR_ID | HIRES_KEY;
  313.     }
  314.  
  315.     if ( NOT( tf = OpenDiskFont( &MainFont )))
  316.         CopyMem(( char *)&Topaz80, (char *)&MainFont, (long)sizeof( struct TextAttr ));
  317.  
  318.     if ( MainScreen ) {
  319.         FreeScreenInfo( MainScreen );
  320.         CloseScreen( MainScreen );
  321.         MainScreen = 0l;
  322.     }
  323.  
  324.     if ( NOT( MainScreen = OpenScreenTagList( 0l, MainSTags )))
  325.         return FALSE;
  326.  
  327.     if( NOT GetScreenInfo( MainScreen ))
  328.         return FALSE;
  329.  
  330.     nwTags[7 ].ti_Data =    (Tag)MainScreen;
  331.     nwTags[10].ti_Data =    (Tag)MainScreen->Width;
  332.     nwTags[11].ti_Data =    (Tag)MainScreen->Height;
  333.  
  334.     if ( NOT( MainWindow = OpenWindowTagList( 0l, nwTags )))
  335.         return FALSE;
  336.  
  337.     SetMouseQueue( MainWindow, 1l );
  338.  
  339.     if ( mm )   MouseMove( MainWindow, TRUE );
  340.  
  341.     if ( NOT( MainMenus = CreateMenus( Menus, GTMN_FrontPen, 0l, TAG_DONE )))
  342.         return FALSE;
  343.  
  344.     MainRP = MainWindow->RPort;
  345.     LayoutMenus( MainMenus, MainVisualInfo, GTMN_TextAttr, &Topaz80, TAG_DONE );
  346.     SetMenuStrip( MainWindow, MainMenus );
  347.  
  348.     if ( tf )
  349.         CloseFont( tf );
  350.  
  351.     return( RemakeAllGadgets());
  352. }
  353.  
  354. /*
  355.  * --- Open/Close workbench screen.
  356.  */
  357. void DoWBench( void )
  358. {
  359.     if ( NOT WBenchClose ) {
  360.         if ( CloseWorkBench()) {
  361.             WBenchClose = TRUE;
  362.             Forbid();
  363.             strcpy( MainWBStatus, "Open Workbench" );
  364.             Permit();
  365.             return;
  366.         } else
  367.             MyRequest( "GadToolsBox message:", "CONTINUE", "Can't close the Workbench !" );
  368.     } else {
  369.         if( OpenWorkBench()) {
  370.             WBenchClose = FALSE;
  371.             Forbid();
  372.             strcpy( MainWBStatus, "Close Workbench" );
  373.             Permit();
  374.             ScreenToFront( MainScreen );
  375.             return;
  376.         } else
  377.             MyRequest( "GadToolsBox message:", "CONTINUE", "Can't open the Workbench !" );
  378.     }
  379. }
  380.  
  381. /*
  382.  * --- This routine looks through the list to find
  383.  * --- the ExtNewGadget structure of the gadget.
  384.  */
  385. struct ExtNewGadget *FindExtGad( struct Gadget *gadget )
  386. {
  387.     struct ExtNewGadget *eng;
  388.  
  389.     eng = Gadgets.gl_First;
  390.  
  391.     while ( eng ) {
  392.         if ( eng->en_Gadget == gadget ) break;
  393.         eng = eng->en_Next;
  394.     }
  395.  
  396.     return( eng );
  397. }
  398.  
  399. /*
  400.  * --- Renumber all gadget ID's and update the
  401.  * --- non user labels.
  402.  */
  403. void Renumber( void )
  404. {
  405.     struct ExtNewGadget *eng;
  406.     UWORD                id = CountFrom;
  407.  
  408.     for ( eng = Gadgets.gl_First; eng->en_Next; eng = eng->en_Next ) {
  409.         if (( eng->en_SpecialFlags & EGF_USERLABEL ) != EGF_USERLABEL )
  410.             sprintf( &eng->en_SourceLabel[0], "Gadget%ld", id - CountFrom );
  411.         eng->en_NewGadget.ng_GadgetID = id++;
  412.     }
  413. }
  414.  
  415. /*
  416.  * --- Append ".G" to the filename if it isn't there.
  417.  */
  418. void CheckSuffix( void )
  419. {
  420.     UBYTE   buf[10];
  421.     UWORD   len;
  422.  
  423.     if ( strlen( bi_SFile )) {
  424.         len = strlen( MainFileName ) - 2;
  425.  
  426.         strcpy( buf, &MainFileName[ len ] );
  427.  
  428.         if ( NOT Stricmp( buf, MainExtension ))
  429.             return;
  430.  
  431.         strcat( MainFileName, MainExtension );
  432.         strcat( bi_SFile, MainExtension );
  433.         strcat( bi_LFile, MainExtension );
  434.     }
  435. }
  436.  
  437. /*
  438.  * --- Attach a StringExtend structure to a string gadget.
  439.  */
  440. void SetStringGadget( struct Gadget *g )
  441. {
  442.     struct StringInfo   *si;
  443.  
  444.     si = ( struct StringInfo * )g->SpecialInfo;
  445.  
  446.     g->Activation |= GACT_STRINGEXTEND;
  447.     si->Extension  = &Sextend;
  448. }
  449.  
  450. /*
  451.  * --- Remove all gadgets from the window to
  452.  * --- let the program change the contents and list.
  453.  */
  454. void RemoveAllGadgets( void )
  455. {
  456.     if ( MainGList ) {
  457.         if ( GadgetsOn ) {
  458.             RemoveGList( MainWindow, MainGList, -1l );
  459.             GadgetsOn = FALSE;
  460.         }
  461.         FreeGadgets( MainGList );
  462.         MainGList = 0l;
  463.     }
  464. }
  465.  
  466. /*
  467.  * --- Re-create all gadgets in the list.
  468.  */
  469. long RemakeAllGadgets( void )
  470. {
  471.     struct ExtNewGadget *eng;
  472.     struct Gadget       *g, *lock;
  473.  
  474.     ClearWindow();
  475.  
  476.     if ( g = CreateContext( &MainGList )) {
  477.         for ( eng = Gadgets.gl_First; eng->en_Next; eng = eng->en_Next ) {
  478.             eng->en_NewGadget.ng_TextAttr   = &MainFont;
  479.             eng->en_NewGadget.ng_VisualInfo = MainVisualInfo;
  480.  
  481.             SizeAGadget( eng );
  482.  
  483.             if (( eng->en_SpecialFlags & EGF_NEEDLOCK ) == EGF_NEEDLOCK )
  484.                 SetTagData( eng->en_Tags, GTLV_ShowSelected, (Tag)eng->en_Prev->en_Gadget );
  485.  
  486.             g = CreateGadgetA( eng->en_Kind, g, &eng->en_NewGadget, eng->en_Tags );
  487.  
  488.             eng->en_Gadget = g;
  489.         }
  490.  
  491.         if ( g ) {
  492.             AddGList( MainWindow, MainGList, -1l, -1l, 0l );
  493.             RefreshWindow();
  494.             GadgetsOn = TRUE;
  495.             return TRUE;
  496.         }
  497.     }
  498.  
  499.     MyRequest( "Oh oh...", "CONTINUE", "BIG TROUBLE!\nCould not create the gadgets !" );
  500.  
  501.     return FALSE;
  502. }
  503.  
  504. /*
  505.  * --- Deallocate a ExtNewGadget structure plus
  506.  * --- any additional memory a specific kind
  507.  * --- of gadget takes up.
  508.  */
  509. void FreeExtGad( struct ExtNewGadget *eng )
  510. {
  511.     UWORD       cnt;
  512.  
  513.     if ( eng->en_Tags )
  514.         FreeTagList( eng->en_Tags, eng->en_NumTags );
  515.  
  516.     switch( eng->en_Kind ) {
  517.  
  518.         case    STRING_KIND:
  519.             if ( eng->en_DefString )
  520.                 FreeItem( Chain, eng->en_DefString, (long)strlen( eng->en_DefString ) + 1 );
  521.             break;
  522.  
  523.         case    LISTVIEW_KIND:
  524.             FreeList( &eng->en_Entries );
  525.             break;
  526.  
  527.         case    MX_KIND:
  528.         case    CYCLE_KIND:
  529.             for ( cnt = 0; cnt < 23; cnt++ ) {
  530.                 if ( eng->en_Labels[ cnt ] )
  531.                     FreeItem( Chain, eng->en_Labels[ cnt ], strlen( eng->en_Labels[ cnt ] ) + 1 );
  532.             }
  533.             break;
  534.  
  535.         case    SLIDER_KIND:
  536.             if ( eng->en_LevelFormat )
  537.                 FreeItem( Chain, eng->en_LevelFormat, (long)strlen( eng->en_LevelFormat ) + 1 );
  538.             break;
  539.     }
  540.  
  541.     FreeItem( Chain, eng, (long)sizeof( struct ExtNewGadget ));
  542. }
  543.  
  544. /*
  545.  * --- Switch on/off the WFLG_REPORTMOUSE flags.
  546.  * --- (Not using ReportMouse())
  547.  */
  548. long MouseMove( struct Window *wnd, long on )
  549. {
  550.     if ( NOT on ) {
  551.         Forbid();
  552.         if (( wnd->Flags & WFLG_REPORTMOUSE ) == WFLG_REPORTMOUSE ) {
  553.             wnd->Flags &= ~WFLG_REPORTMOUSE;
  554.             Permit();
  555.             return TRUE;
  556.         }
  557.         Permit();
  558.         return FALSE;
  559.     } else {
  560.         Forbid();
  561.         wnd->Flags |= WFLG_REPORTMOUSE;
  562.         Permit();
  563.         if (( MainPrefs.pr_PrefFlags0 & PRF_COORDS ) == PRF_COORDS )
  564.             UpdateCoords( 0l, 0, 0, 0, 0 );
  565.         return TRUE;
  566.     }
  567. }
  568.  
  569. /*
  570.  * --- FlipFlop a boolean and checkbox gadget ( if specified )
  571.  */
  572. void FlipFlop( struct Window *wnd, struct Gadget **list, long index, BOOL *val )
  573. {
  574.     if ( *val ) *val = FALSE;
  575.     else        *val = TRUE;
  576.  
  577.     if ( list )
  578.         GT_SetGadgetAttrs( list[ index ], wnd, 0l, GTCB_Checked, (Tag)*val, TAG_DONE );
  579. }
  580.  
  581. /*
  582.  * --- EnableGadget a gadget ( if specified )
  583.  */
  584. void EnableGadget( struct Window *wnd, struct Gadget **list, long index, BOOL val )
  585. {
  586.         BOOL    off;
  587.  
  588.         if ( val )  off = FALSE;
  589.         else        off = TRUE;
  590.  
  591.         GT_SetGadgetAttrs( list[ index ], wnd, 0l, GA_Disabled, (Tag)off, TAG_DONE );
  592. }
  593.  
  594. /*
  595.  * --- Convert ListViewNodes to gadget labels.
  596.  */
  597. long ListToLabels( struct List *list, struct ExtNewGadget *eng )
  598. {
  599.     struct ListViewNode *node;
  600.     UWORD                cnt, num = 0;
  601.  
  602.     setmem(( void * )&eng->en_Labels[0], 100l, 0l );
  603.  
  604.     for ( node = list->lh_Head, cnt = 0; node->ln_Succ; node = node->ln_Succ, cnt++ ) {
  605.         if (  eng->en_Labels[ cnt ] = AllocItem( Chain, strlen( &node->ln_NameBytes[0] ) + 1, MEMF_PUBLIC )) {
  606.             strcpy( eng->en_Labels[ cnt ], &node->ln_NameBytes[0] );
  607.             num++;
  608.         } else {
  609.             FreeList( list );
  610.             return FALSE;
  611.         }
  612.     }
  613.     FreeList( list );
  614.     return((long) num );
  615. }
  616.  
  617. /*
  618.  * --- Convert gadget labels to ListViewNodes.
  619.  */
  620. long LabelsToList( struct List *list, struct ExtNewGadget *eng )
  621. {
  622.     struct ListViewNode *node;
  623.     UWORD                cnt, num = 0;
  624.  
  625.     NewList( list );
  626.  
  627.     for ( cnt = 0; cnt < 24;  cnt ++ ) {
  628.         if ( eng->en_Labels[ cnt ] ) {
  629.             if ( node = MakeNode( eng->en_Labels[  cnt ] )) {
  630.                 AddTail( list, ( struct Node * )node );
  631.                 FreeItem( Chain, eng->en_Labels[ cnt ], strlen( eng->en_Labels[ cnt ] ) + 1 );
  632.                 eng->en_Labels[ cnt ] = 0l;
  633.                 num++;
  634.             } else {
  635.                 FreeList( list );
  636.                 return FALSE;
  637.             }
  638.         }
  639.     }
  640.     return((long)num);
  641. }
  642.  
  643. /*
  644.  * --- Size the gadget to it's minimum width when to small.
  645.  */
  646. void SizeAGadget( struct ExtNewGadget *eng )
  647. {
  648.     struct NewGadget    *ng = &eng->en_NewGadget;
  649.  
  650.     switch( eng->en_Kind ) {
  651.  
  652.         case    BUTTON_KIND:
  653.         case    CYCLE_KIND:
  654.                 if ( ng->ng_Width  < ( MainScreen->RastPort.TxWidth + 8 ))
  655.                     ng->ng_Width  = MainScreen->RastPort.TxWidth + 8;
  656.                 if ( ng->ng_Height < ( MainScreen->RastPort.TxHeight + 3 ))
  657.                     ng->ng_Height = MainScreen->RastPort.TxHeight + 3;
  658.                 break;
  659.  
  660.         case    INTEGER_KIND:
  661.         case    STRING_KIND:
  662.                 if ( ng->ng_Width < ( MainScreen->RastPort.TxWidth + 13 ))
  663.                     ng->ng_Width = MainScreen->RastPort.TxWidth + 13;
  664.                 if ( ng->ng_Height < ( MainScreen->RastPort.TxHeight + 4 ))
  665.                     ng->ng_Height = MainScreen->RastPort.TxHeight + 4;
  666.  
  667.                 if ( eng->en_Kind == STRING_KIND ) {
  668.                     if (( eng->en_SpecialFlags & EGF_ISLOCKED ) == EGF_ISLOCKED )
  669.                         eng->en_NewGadget.ng_Width = eng->en_Next->en_NewGadget.ng_Width;
  670.                 }
  671.  
  672.                 break;
  673.  
  674.         case    LISTVIEW_KIND:
  675.                 if ( eng->en_ScrollWidth ) {
  676.                     if ( ng->ng_Width < ( eng->en_ScrollWidth + 14 ))
  677.                         ng->ng_Width = eng->en_ScrollWidth + 14;
  678.                 } else {
  679.                     if ( ng->ng_Width < 30 )
  680.                         ng->ng_Width = 30;
  681.                 }
  682.                 if ( ng->ng_Height < (( MainScreen->RastPort.TxHeight << 1 ) + 9 ))
  683.                     ng->ng_Height = ( MainScreen->RastPort.TxHeight << 1 ) + 9;
  684.                 break;
  685.  
  686.         case    PALETTE_KIND:
  687.                 if ( MyTagInArray( GTPA_IndicatorWidth, eng->en_Tags )) {
  688.                     if ( ng->ng_Width < ( eng->en_IndicatorSize + 20 ))
  689.                         ng->ng_Width = eng->en_IndicatorSize + 20;
  690.                     if ( ng->ng_Height < 20 )
  691.                         ng->ng_Height = 20;
  692.                 } else {
  693.                     if ( ng->ng_Width < 20 )
  694.                         ng->ng_Width = 20;
  695.                     if ( ng->ng_Height < (( eng->en_IndicatorSize >> 1 ) + 20 ))
  696.                         ng->ng_Height = ( eng->en_IndicatorSize >> 1 ) + 20;
  697.                 }
  698.                 break;
  699.  
  700.         case    SCROLLER_KIND:
  701.                 if ( NOT MyTagInArray( PGA_Freedom, eng->en_Tags )) {
  702.                     if ( ng->ng_Width < (( eng->en_ArrowSize << 1 ) + 10 ))
  703.                         ng->ng_Width = ( eng->en_ArrowSize << 1 ) + 10;
  704.                     if ( ng->ng_Height < 8)
  705.                         ng->ng_Height = 8;
  706.                 } else {
  707.                     if ( ng->ng_Width < 10 )
  708.                         ng->ng_Width = 10;
  709.                     if ( ng->ng_Height < ((eng->en_ArrowSize << 1) + 10 ))
  710.                         ng->ng_Height = (eng->en_ArrowSize << 1) + 10;
  711.                 }
  712.                 break;
  713.  
  714.         case    SLIDER_KIND:
  715.                 if ( ng->ng_Width  < 12 ) ng->ng_Width  = 12;
  716.                 if ( ng->ng_Height < 8 ) ng->ng_Height = 8;
  717.                 break;
  718.     }
  719. }
  720.  
  721. /*
  722.  * --- Copy the contents of one list to another.
  723.  */
  724. long CopyList( struct ExtNewGadget *src, struct ExtNewGadget *dst )
  725. {
  726.     struct ListViewNode *node, *tmp;
  727.  
  728.     NewList( &dst->en_Entries );
  729.  
  730.     for( tmp = src->en_Entries.lh_Head; tmp->ln_Succ; tmp = tmp->ln_Succ ) {
  731.         if( node = MakeNode( tmp->ln_Name )) {
  732.             node->ln_UserData[0] = tmp->ln_UserData[0];
  733.             AddTail( &dst->en_Entries, ( struct Node * )node);
  734.         } else
  735.             return FALSE;
  736.     }
  737.     SetTagData( dst->en_Tags, GTLV_Labels, (Tag)&dst->en_Entries );
  738.     return TRUE;
  739. }
  740.  
  741. /*
  742.  * --- Copy the labels of one gadget to another.
  743.  */
  744. long CopyLabels( struct ExtNewGadget *src, struct ExtNewGadget *dst, long t )
  745. {
  746.     UWORD   cnt;
  747.  
  748.     for ( cnt = 0; cnt < 24; cnt ++ ) {
  749.         if ( src->en_Labels[ cnt ] ) {
  750.             if ( dst->en_Labels[ cnt ] = AllocItem( Chain, (long)strlen( src->en_Labels[ cnt ] ) + 1, MEMF_PUBLIC ))
  751.                 strcpy( dst->en_Labels[ cnt ], src->en_Labels[ cnt ] );
  752.             else
  753.                 return FALSE;
  754.         }
  755.     }
  756.  
  757.     if ( t )    SetTagData( dst->en_Tags, GTMX_Labels, (Tag)&dst->en_Labels[0] );
  758.     else        SetTagData( dst->en_Tags, GTCY_Labels, (Tag)&dst->en_Labels[0] );
  759.  
  760.     return TRUE;
  761. }
  762.  
  763. /*
  764.  * --- Check for a file existance. If it exists put a requester up
  765.  * --- and ask to over write when mode if MODE_NEWFILE.
  766.  */
  767. BPTR MyOpen( long mode )
  768. {
  769.     BPTR    file;
  770.     long    rc = TRUE;
  771.  
  772.     if ( mode == MODE_NEWFILE ) {
  773.         if ( file = Open( MainFileName, MODE_OLDFILE )) {
  774.             AlertUser( TRUE );
  775.             rc = MyRequest( "Excuse me...", "YEP|NOOOO..", "---> %s <---\n\nFile already exists !\nOverwrite it ?", MainFileName );
  776.             AlertUser( FALSE );
  777.             Close( file );
  778.         }
  779.     }
  780.  
  781.     if ( rc ) return ( Open( MainFileName, mode ));
  782.     return( NULL );
  783. }
  784.  
  785. /*
  786.  * --- Delete all gadgets.
  787.  */
  788. void DeleteAllGadgets( void )
  789. {
  790.     struct ExtNewGadget *eng;
  791.  
  792.     RemoveAllGadgets();
  793.  
  794.     while ( eng = ( struct ExtNewGadget * )RemHead(( struct List * )&Gadgets ))
  795.         FreeExtGad( eng );
  796.  
  797.     NewList(( struct List * )&Gadgets );
  798. }
  799.  
  800. /*
  801.  * --- Check if a '/' or a ':' is appended to the dirname.
  802.  */
  803. void CheckDirExtension( void )
  804. {
  805.     UWORD   len = strlen( MainFileName );
  806.  
  807.     if ( len ) {
  808.         len--;
  809.         if ( MainFileName[ len ] != '/' && MainFileName[ len ] != ':' )
  810.             strcat( MainFileName, "/" );
  811.     }
  812. }
  813.  
  814. /*
  815.  * --- Refresh the window.
  816.  */
  817. void RefreshWindow( void )
  818. {
  819.     RefreshWindowFrame( MainWindow );
  820.  
  821.     if ( MainGList ) {
  822.         RefreshGList( MainGList, MainWindow, 0l, -1l );
  823.         GT_RefreshWindow( MainWindow, 0l );
  824.     }
  825.  
  826.     if ( WindowTxt )
  827.         PrintIText( MainRP, WindowTxt, 0l, 0l );
  828. }
  829.  
  830. /*
  831.  * --- (Re)Set the allert color.
  832.  */
  833. void AlertUser( long how )
  834. {
  835.     if ( how ) {
  836.         AlertCol = GetRGB4( MainScreen->ViewPort.ColorMap, 0l );
  837.         SetRGB4( &MainScreen->ViewPort, 0l, 0x0F, 0x08, 0x06 );
  838.     } else
  839.         LoadRGB4( &MainScreen->ViewPort, &AlertCol, 2l );
  840. }
  841.  
  842. /*
  843.  * --- Quit the program.
  844.  */
  845. void Quit( void )
  846. {
  847.     if ( NOT Saved ) {
  848.         AlertUser( TRUE );
  849.         if ( MyRequest( "!!! RED ALERT !!!", "Go Ahead|Nooooo!!!","Changes not saved !\n   Quit anyway ?" ))
  850.             goto quit;
  851.     } else {
  852.         AlertUser( TRUE );
  853.         if ( MyRequest( "Specify...", "YES|NO", "  Are you sure\nYou want to quit?"))
  854.             goto quit;
  855.     }
  856.     AlertUser( FALSE );
  857.     return;
  858.     quit:
  859.     QuitProgram( 0l );
  860. }
  861.  
  862. /*
  863.  * --- Perform formatted output to a file.
  864.  */
  865. long MyFPrintf( BPTR fh, UBYTE *format, ... )
  866. {
  867.     va_list     args;
  868.     long        ret;
  869.  
  870.     va_start( args, format );
  871.  
  872.     ret = VFPrintf( fh, format, args );
  873.  
  874.     va_end( args );
  875.  
  876.     return( ret );
  877. }
  878.  
  879. /*
  880.  * Count the gadgets in the list.
  881.  */
  882. long CountGadgets( void )
  883. {
  884.     struct ExtNewGadget *eng;
  885.     long                 num;
  886.  
  887.     for ( eng = Gadgets.gl_First, num = 0l; eng->en_Next; eng = eng->en_Next, num++ );
  888.  
  889.     return( num );
  890. }
  891.